function changeCategory(obj,category_id,sub_category_id){
	
	//alert($(obj).val());
	var new_category_id = $(obj).val();
	$("#loading_img_"+category_id+'_'+sub_category_id).show();
	 $.ajax({
            type: "POST",
			url: "../lib/actions/changeCategory.php",
            data: {
                category_id: category_id,
				sub_category_id: sub_category_id,
				new_category_id: new_category_id
            },
            cache: false,
            success: function(data){	
				if (data) {
                    //alert('Success!');
                    reloadSubscription();
                }
                else {
                   // alert('Failed!');
                }
            }
        });
}

function addFeed(URL){
	$.ajax({
            type: "POST",
			url: "../lib/actions/addFeed.php",
			dataType:"json",
            data: {
                QUERY_URL:$('#feedUrl').val()
            },
            cache: false,
            success: function(data){		
				alert(data.msg);		
              if(data.update_status){
				reloadSubscription();
			  }
            }
        });
}

function addCategory(URL){
	$.ajax({
            type: "POST",
			url: "../lib/actions/addCategory.php",
			dataType:"json",
            data: {
                NEW_CATEGORY:$('#new_category').val()
            },
            cache: false,
            success: function(data){		
				alert(data.msg);		
              if(data.update_status){
				reloadCategory();
			  }
            }
        });
}

function renameTitle(category_id,sub_category_id){
	//alert($("#feed_new_title_"+sub_category_id).val());
	var new_title = $("#feed_new_title_"+category_id+'_'+sub_category_id).val();
	$("#feed_title_"+category_id+'_'+sub_category_id).html('<img src="../styles/images/ui-anim_basic_16x16.gif"/>');
	 $.ajax({
            type: "POST",
			url: "../lib/actions/renameTitle.php",
            data: {
                category_id: category_id,
				sub_category_id: sub_category_id,
				new_title: new_title
            },
            cache: false,
            success: function(data){				
               $("#feed_title_"+category_id+'_'+sub_category_id).html(data);
			   $("#rename_button_"+category_id+'_'+sub_category_id).html('<input type="button" value="重新命名" onclick="showRenameTitle(\''+category_id+'\',\''+sub_category_id+'\')"/>');
            }
        });
}

function renameCategoryTitle(category_id){
	
	var new_title = $("#category_new_title_"+category_id).val();
	$("#category_title_"+category_id).html('<img src="../styles/images/ui-anim_basic_16x16.gif"/>');
	 $.ajax({
            type: "POST",
			url: "../lib/actions/renameCategoryTitle.php",
            data: {
                category_id: category_id,
				new_title: new_title
            },
            cache: false,
            success: function(data){				
               $("#category_title_"+category_id).html(data);
			   $("#rename_button_"+category_id).html('<input type="button" value="重新命名" onclick="showCategoryRenameTitle(\''+category_id+'\')"/>');
            }
        });
}

function reloadSubscription(){
	$("#subscribe_list").html('<img src="../styles/images/ui-anim_basic_16x16.gif"/>');
	$.ajax({
            type: "POST",
			url: "../lib/actions/reloadSubscription.php",
            dataType:"json",
            cache: false,
            success: function(data){	
				if(data.status){
					$("#subscribe_list").html(data.msg);
				}
				else{
					location.reload();
				}	
            }
        });
}
function reloadCategory(){
	$("#category_list").html('<img src="../styles/images/ui-anim_basic_16x16.gif"/>');
	$.ajax({
            type: "POST",
			url: "../lib/actions/reloadCategory.php",
            dataType:"json",
            cache: false,
            success: function(data){	
				if(data.status){
					$("#category_list").html(data.msg);
				}
				else{
					location.reload();
				}	
            }
        });
}

function deleteSubscription(category_id, sub_category_id){
    if (confirm("確定要刪除此筆訂閱嗎?")) {
        $("#unsubscribe_" + category_id + '_' + sub_category_id).html('<img src="../styles/images/ui-anim_basic_16x16.gif"/>');
        $.ajax({
            type: "POST",
            url: "../lib/actions/deleteSubscription.php",
            data: {
                category_id: category_id,
                sub_category_id: sub_category_id
            },
            cache: false,
            success: function(data){
                if (data) {
                    reloadSubscription();
                }
                else {
                    alert('Failed!');
                    $("#unsubscribe_" + category_id + '_' + sub_category_id).html('<img src="styles/images/icons/unsubscribe.png" style="cursor:pointer;" onclick="deleteSubscription(\'' + category_id + '\',\'' + sub_category_id + '\')"/>');
                }
            }
        });
    }
}

function deleteCategory(category_id, sub_category_id){
    if (confirm("確定要刪除此筆分類嗎?")) {
        $("#deleteCategory_" + category_id).html('<img src="../styles/images/ui-anim_basic_16x16.gif"/>');
        $.ajax({
            type: "POST",
            url: "../lib/actions/deleteCategory.php",
			dataType:"json",
            data: {
                category_id: category_id
            },
            cache: false,
            success: function(data){
				alert(data.msg);
                if (data.delete_status) {
                    window.location.reload();
                }
                else {
                    $("#deleteCategory_" + category_id ).html('<img src="styles/images/icons/unsubscribe.png" style="cursor:pointer;" onclick="deleteCategory(\'' + category_id + '\')"/>');
                }
            }
        });
    }
}

function gotoNextPage(start,q){
	
	$("#next_loading").html('<img src="../styles/images/ui-anim_basic_16x16.gif"/>');
	 $.ajax({
            type: "POST",
			url: "../lib/searchGoogleAPI.php",
            data: {
                start: start,
				q: q
            },
            cache: false,
            success: function(data){				
				$("#more_result_button").remove();
				$("#google_result").append(data);		
				$("#next_loading").html('');	
				getADs('ads_right_bottom',10,q);
				highlightWords();
				
            }
        });
}
function getADs(id,ad_num){
	//$("#"+id).html('<div class="alignCenter"><img src="../styles/images/ui-anim_basic_16x16.gif"/></div>');
	
	 $.ajax({
            type: "POST",
            url: "../lib/ads.php",
            data: {
                ad_num: ad_num
            },
            cache: false,
            success: function(data){				
                //$("#"+id).html(data);
				$("#"+id).append(data);	
				highlightWords();		
            }
        });
}
function getGoogleResult(q){
	$("#loading_img").bind("ajaxSend", function(){			
           $(this).show();
    }).bind("ajaxComplete", function(){
           $(this).hide();
    });
	
	$.ajax({
            type: "POST",
            url: "../lib/searchGoogleAPI.php",
            data: {
                q: q
            },
            cache: false,
            success: function(data){
                $("#page").append(data);		
            }
        });	
}
function getWords(obj_id){	
	var word_string = $("#"+obj_id+"_string").html();	
    $.ajax({
            type: "POST",
            url: "lib/actions/getWords.php",
            data: {
                words: word_string
            },
            cache: false,
            success: function(data){
                $("#"+obj_id).html(data);
            }
        });
}
function getRSSByCategory(category,key,refreshpage,more){	
	if($('#get_more').length>0){
		$('#get_more').remove();
	}
		
	$("#loading_img").bind("ajaxSend", function(){
			if(refreshpage){
				$("#main_info").html('');
			}
			
           $(this).show();
    }).bind("ajaxComplete", function(){
		   $("#main_info").fadeIn("slow");
           $(this).hide();
    });
	
    $.ajax({
            type: "POST",
            url: "lib/actions/getRSSByCategory.php",
			dataType:"json",
            data: {
                category: category,
                key: key,
				more: more
            },
            cache: false,
            success: function(data){
				if(data.status){
					$("#main_info").append(data.msg);	
				}
                else{
					if(data.error_code != 0){
						alert(data.msg);
					}
					else{
						location.replace('http://www.ulikewall.com/');
					}
				}
            }
        });
}

function getLocalContent(obj){	
	//alert($(".sideMenu").attr('tagName'));
	
   // alert(obj.attr("tagName"));
	
	if($('#get_more').length>0){
		$('#get_more').remove();
	}
		
	$("#loading_img").bind("ajaxSend", function(){
		$("#main_info").html('');
        $(this).show();
    }).bind("ajaxComplete", function(){
		   $("#main_info").fadeIn("slow");
           $(this).hide();
    });
	
    $.ajax({
            type: "POST",
            url: "lib/actions/getLocalContent.php",
			dataType:"html",
            data: {
                URL:obj.attr("original")
            },
            cache: false,
            success: function(data){
				$("#main_info").append(data);
				//$("p").addClass("myClass yourClass");
            }
        });
		
	return false;
}

function getMoreBeauty(id,pageuntil){
	if($('#next_page').length>0){
		$('#next_page').remove();
	}
	
	$("#loading_img").bind("ajaxSend", function(){
           $(this).show();
    }).bind("ajaxComplete", function(){
           $(this).hide();
    });
	
    $.ajax({
            type: "POST",
            url: "lib/actions/getMoreBeauty.php",
            data: {
                ID: id,
                PAGE_UNTIL: pageuntil
            },
            cache: false,
            success: function(data){
                $("#main_wall").append(data);
            }
            
        });
}


